import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sn

from sklearn.datasets import load_breast_cancer

# data sets
data = load_breast_cancer()
# print(data)

x = data.data
y = data.target

plt.scatter(x[:, 0], x[:, 23])
plt.show()

x0 = x[:, 0]
x23 = x[:, 23]

x0_reshape = x0.reshape(-1, 1)
x23_reshape = x23.reshape(-1, 1)

from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import StandardScaler

degree = 3
# 这里将三个处理步骤进行了封装，将数据传入poly_reg之后，将会智能地沿着该管道进行处理
poly_reg = Pipeline([
    ("poly", PolynomialFeatures(degree=degree)),
    ("std_scaler", StandardScaler()),
    ("lin_reg", LinearRegression())
])

poly_reg.fit(x0_reshape, x23_reshape)

print("截距: ", poly_reg.named_steps["lin_reg"].intercept_)
print("权重: ", poly_reg.named_steps["lin_reg"].coef_)

x23_reshape_predict = poly_reg.predict(x0_reshape)

# data fit
plt.scatter(x0, x23)
plt.plot(np.sort(x0), x23_reshape_predict[np.argsort(x0)], color='r')
plt.show()

# ============
x023 = np.c_[x0, x23]

columns = ['columns0', 'columns23']  # 横轴标签
df_x023 = pd.DataFrame(x023, columns=columns)

# pearsonCorr and heatmap
df1 = df_x023.corr()
print("pearsonCorr: ", df_x023.corr())
sn.heatmap(df1, annot=True)
plt.show()

# jointplot and distplot
sn.jointplot(x='columns0', y='columns23', data=df_x023)
plt.show()

ax = sn.distplot(x0)
plt.show()

